//
// Created by 邹嘉旭 on 2025/8/6.
//

#include "util/log.h"


static const char* interface_type_to_string(interface_type_t intf) {
    return  interface_str[intf];
}

void cn_log(const char *ele_tag, interface_type_t intf, int level, const char *file, int line, const char *fmt, ...) {
    char new_fmt[512];
    char ele_fmt[16] = ""; // 网元标识缓冲区
    char intf_fmt[16] = ""; // 接口标识缓冲区
    // if (ele_tag != ELEMENT_INVALID && ele_tag <= ELEMENT_MAX) {
    //     const char *ele_str = element_type_to_string(ele_tag);
    //     snprintf(ele_fmt, sizeof(ele_fmt), "[%s]", ele_str);
    // }
    snprintf(ele_fmt, sizeof(ele_fmt), "[%s]", ele_tag);
    if (intf != INTERFACE_UNDEFINED && intf <= INTERFACE_MAX) {
        const char *intf_str = interface_type_to_string(intf);
        snprintf(intf_fmt, sizeof(intf_fmt), "(%s)", intf_str);
    }
    snprintf(new_fmt, sizeof(new_fmt), "%s%s %s", ele_fmt, intf_fmt, fmt);

    // 处理可变参数
    va_list args;
    va_start(args, fmt);
    log_va(level, file, line, new_fmt, &args);
    va_end(args);
}

void cn_log_buffer(const char *ele_tag, interface_type_t intf, int level, const char *file, int line, const char *msg_head, const uint8_t *buf, size_t len) {
    char new_fmt[512];
    char ele_fmt[16] = ""; // 网元标识缓冲区
    char intf_fmt[16] = ""; // 接口标识缓冲区
    // if (ele_tag != ELEMENT_INVALID && ele_tag <= ELEMENT_MAX) {
    //     const char *ele_str = element_type_to_string(ele_tag);
    //     snprintf(ele_fmt, sizeof(ele_fmt), "[%s]", ele_str);
    // }
    snprintf(ele_fmt, sizeof(ele_fmt), "[%s]", ele_tag);
    if (intf != INTERFACE_UNDEFINED && intf <= INTERFACE_MAX) {
        const char *intf_str = interface_type_to_string(intf);
        snprintf(intf_fmt, sizeof(intf_fmt), "(%s)", intf_str);
    }
    snprintf(new_fmt, sizeof(new_fmt), "%s%s %s", ele_fmt, intf_fmt, msg_head);

    log_print_buffer(level, file, line, new_fmt, buf, len);
}
